﻿using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C4F.DevKit.Contacts.Data.SimpleContactDataSetTableAdapters;

namespace C4F.DevKit.Contacts.Data
{
    public class SimpleContactDataProvider
    {
        public SimpleContactDataSet Load()
        {
            SimpleContactDataSet ds = new SimpleContactDataSet();
            SimpleContactTableAdapter adptrSimpleContact = new SimpleContactTableAdapter();

            using (SqlConnection conn = GetConnection())
            {
                adptrSimpleContact.Connection = conn;

                try
                {
                    conn.Open();

                    try
                    {
                        adptrSimpleContact.Fill(ds.SimpleContact);
                        return ds;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public SimpleContactDataSet LoadByID(Guid id)
        {
            SimpleContactDataSet ds = new SimpleContactDataSet();
            SimpleContactTableAdapter adptrSimpleContact = new SimpleContactTableAdapter();

            using (SqlConnection conn = GetConnection())
            {
                adptrSimpleContact.Connection = conn;

                try
                {
                    conn.Open();

                    try
                    {
                        adptrSimpleContact.FillByContactID(ds.SimpleContact, id);
                        return ds;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public void Save(SimpleContactDataSet ds)
        {
            SimpleContactTableAdapter adptrSimpleContact = new SimpleContactTableAdapter();

            using (SqlConnection conn = GetConnection())
            {
                adptrSimpleContact.Connection = conn;

                try
                {
                    conn.Open();

                    using (SqlTransaction dbTrans = adptrSimpleContact.Connection.BeginTransaction())
                    {
                        try
                        {
                            adptrSimpleContact.Transaction = dbTrans;
                            adptrSimpleContact.Update(ds.SimpleContact);
                            dbTrans.Commit();
                        }
                        catch (Exception)
                        {
                            dbTrans.Rollback();
                            throw;
                        }
                    }
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public SqlConnection GetConnection()
        {
            return new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data\Contacts.mdf;Integrated Security=True;User Instance=True");
        }

    }
}
